package com.g4.shoppingback.controller;


import com.g4.shoppingback.annotation.WriteLog;
import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.common.Result;
import com.g4.shoppingback.pojo.DdKindType;
import com.g4.shoppingback.pojo.DdProductInfo;
import com.g4.shoppingback.service.IDdKindTypeService;
import com.g4.shoppingback.service.IDdProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/shoppingback/ddProductInfo")
public class DdProductInfoController {

    @Autowired
    private IDdProductInfoService iDdProductInfoService;

    //入库商品表
    @WriteLog("新增商品")
    @PreAuthorize("hasAnyAuthority('miShop:product:add')")
    @PostMapping
    public Result addPro(@RequestBody DdProductInfo ddProductInfo){

        iDdProductInfoService.addPro(ddProductInfo);

        return new Result(true, "新增商品成功");
    }

    //通过Id删除商品表 商品
    @WriteLog("删除商品")
    @PreAuthorize("hasAnyAuthority('miShop:product:delete')")
    @DeleteMapping("/{id}")
    public Result deletePro(@PathVariable Integer id){

        iDdProductInfoService.deletePro(id);

        return new Result(true, "操作成功");
    }

    //通过选择入库商品生成的主键ID,进行商品的状态更新
    //@PutMapping("/{id}/{saleState}")  商品状态改为商品配置状态修改触发
    public Result updateById(@PathVariable Integer id,
                             @PathVariable Integer saleState){

        iDdProductInfoService.updateById(id,saleState);

        return new Result(true,"更新商品状态成功");
    }


    //通过传入的商品的更新信息更改
    @WriteLog("修改商品")
    @PreAuthorize("hasAnyAuthority('miShop:product:update')")
    @PutMapping
    public Result updateWeb(@RequestBody DdProductInfo ddProductInfo){
        iDdProductInfoService.updateWeb(ddProductInfo);

        return new Result(true,"商品修改成功");
    }


    //对商品进行模糊查询
    @PreAuthorize("hasAnyAuthority('miShop:product:find')")
    @GetMapping
    public Result selectByName(PageVo pageVo){

        pageVo = iDdProductInfoService.selectByName(pageVo);

        return Result.ok(pageVo);
    }

    //根据ID查询商品
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){

        DdProductInfo ddProductInfo = iDdProductInfoService.selectById(id);

        return Result.ok(ddProductInfo);
    }

}
